package com.yonyou.pmclouds.messagenotice.constructor.impl;

import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.copyperson.entity.CopyPersonVO;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.repeatwork.entity.RepeatWorkVO;
import com.yonyou.pmclouds.repeatwork.service.rmiitf.IRepeatWorkQuery;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
@Component
public class RepeatWorkMessageConstructor extends AbstractMessageNoticeConstructor {

    private final ProjectSimpleQuery projectSimpleQuery;
    private final IRepeatWorkQuery repeatWorkQuery;
    public RepeatWorkMessageConstructor(IRepeatWorkQuery repeatWorkQuery,ProjectSimpleQuery projectSimpleQuery) {
        this.repeatWorkQuery = repeatWorkQuery;
        this.projectSimpleQuery = projectSimpleQuery;
    }

    @Override
    protected MessageNoticeVO[] generateContent(Entity vo) throws BusinessException {
        MessageNoticeVO noticeVO = coverBusinessVOToMessageVO(vo);
        if (vo == null)
            return new MessageNoticeVO[0];


        RepeatWorkVO repeatWorkVO = repeatWorkQuery.getReport(noticeVO.getRelateKey());
        ProjectVO projectVO = projectSimpleQuery.queryProject(repeatWorkVO.getPkProject());
        List<MessageNoticeVO> noticeVOList = new ArrayList<>();

        String prefix = "";

        //通知内容
        if(repeatWorkVO.getBillstatus()==(BillStatusConst.COMMON_APPROVED_STATUS)) {
            prefix = APPROVAL_SUFFIX_ADOPT_MESSAGE;
        }
        if(repeatWorkVO.getBillstatus()==(BillStatusConst.COMMON_REJECT_STATUS)) {
            prefix = APPROVAL_SUFFIX_REJECT_MESSAGE;
        }

        String messageContent = APPROVAL_PREFIX_MESSAGE + "[" +
                projectVO.getSname()
                + "]的复工单，" + prefix;
        //给制单人发送消息
        noticeVOList.add(getNoticeVO(repeatWorkVO, projectVO.getSname(), messageContent, repeatWorkVO.getCreator()));
        // 给抄送人发送通知
        List<CopyPersonVO> copyPersonVOS = repeatWorkVO.getCopyPersons();
        if (CollectionUtils.isNotEmpty(copyPersonVOS)) {
            messageContent = repeatWorkVO.getProjectName() + "目前已复工，复工日期为"+new SimpleDateFormat("yyyy年MM月dd日").format(repeatWorkVO.getRepeatDate())+",请知晓";
            for (CopyPersonVO copyPersonVO : copyPersonVOS) {
                noticeVOList.add(getNoticeVO(repeatWorkVO, projectVO.getSname(), messageContent, copyPersonVO.getCopyPerson()));
            }
        }
        return noticeVOList.toArray(new MessageNoticeVO[0]);
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.REPEATWORK_BILLTYPE;
    }

//    @Override
//    public MessageNoticeVO[] generateNotice(Object vo) throws BusinessException {
//        return new MessageNoticeVO[0];
//    }

    /**
     * 生成一条消息通知
     *
     * @param vo
     * @param title
     * @param content
     * @param receiver
     * @return
     */
    private MessageNoticeVO getNoticeVO(RepeatWorkVO vo, String title, String content, String receiver) {
        MessageNoticeVO noticeVO = new MessageNoticeVO();
        noticeVO.setRelateType(vo.getBillType());
        noticeVO.setRelateKey(vo.getPkRepeatwork());
        noticeVO.setMessageTitle(title);
        noticeVO.setMessageReceiver(receiver);
        noticeVO.setPkTenant(vo.getPkTenant());
        noticeVO.setMessageContent(content);

        return noticeVO;
    }

}
